
// DataSClientDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DataSClient.h"
#include "DataSClientDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CDataSClientDlg dialog




CDataSClientDlg::CDataSClientDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CDataSClientDlg::IDD, pParent)
	, m_dlgPort(0)
	, m_dlgOutLog(_T(""))
	, m_dlgLog(_T(""))
	, m_dlgIP(_T(""))
	, m_nBid(0)
	, m_nAsk(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

CDataSClientDlg::~CDataSClientDlg()
{

}

void CDataSClientDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT_PORT, m_dlgPort);
	DDX_Text(pDX, IDC_EDIT_OUT_LOG, m_dlgOutLog);
	DDX_Text(pDX, IDC_EDIT_LOG, m_dlgLog);
	DDX_Text(pDX, IDC_EDIT_IP, m_dlgIP);
	DDX_Text(pDX, IDC_EDIT_BID, m_nBid);
	DDX_Text(pDX, IDC_EDIT_ASK, m_nAsk);
}

BEGIN_MESSAGE_MAP(CDataSClientDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK, &CDataSClientDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CDataSClientDlg::OnBnClickedButtonStop)
	ON_BN_CLICKED(IDCANCEL, &CDataSClientDlg::OnBnClickedCancel)
	ON_MESSAGE(WM_OnNewBidAskArrived, &CDataSClientDlg::OnOnNewBidAskArrived)
	ON_MESSAGE(WM_OnNewTickArrived, &CDataSClientDlg::OnOnNewTickArrived)
END_MESSAGE_MAP()


// CDataSClientDlg message handlers

BOOL CDataSClientDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_dlgPort = 2778;
	m_dlgIP = "127.0.0.1";
	m_dlgOutLog = "out.txt";
	m_dlgLog = "";
	m_nBid = 0;
	m_nAsk = 0;
	UpdateData(FALSE);

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDataSClientDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDataSClientDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CDataSClientDlg::OnBnClickedOk()
{
	UpdateData(TRUE);
	theApp.m_port = this->m_dlgPort;
	theApp.m_ip = this->m_dlgIP;
	//m_pDataReceiver.InitMessageLoop();
	m_pDataReceiver.CreateThread();
	//if(theApp.m_DataReceiver.IsRunning())
	//{
	//	theApp.m_DataReceiver.Disconnect();
	//}
	//if(theApp.m_DataReceiver.Connect(this->m_dlgIP, this->m_dlgPort) == false)
	//{
	//	AfxMessageBox(theApp.m_DataReceiver.GetLastError(), MB_OK | MB_ICONERROR);
	//	return;
	//}

}


void CDataSClientDlg::OnBnClickedButtonStop()
{
	m_pDataReceiver.PostThreadMessage(WM_OnDisconnect,(WPARAM)NULL,(LPARAM)NULL);
	// TODO: Add your control notification handler code here
}


void CDataSClientDlg::OnBnClickedCancel()
{
	OnBnClickedButtonStop();
	m_pDataReceiver.PostThreadMessage(WM_QUIT,(WPARAM)NULL,(LPARAM)NULL);

	CDialogEx::OnCancel();
}


afx_msg LRESULT CDataSClientDlg::OnOnNewBidAskArrived(WPARAM wParam, LPARAM lParam)
{
	SBidAsk *bidask = (SBidAsk *)wParam;
	STickerInfo *ticker_info = (STickerInfo *)lParam;
	//m_dlgLog.AppendFormat("Bid: %f\r\n", bidask->ask);
	m_nBid = m_nBid + 1;
	UpdateData(FALSE);
	delete bidask;
	return 0;
}


afx_msg LRESULT CDataSClientDlg::OnOnNewTickArrived(WPARAM wParam, LPARAM lParam)
{
	STick *tick = (STick *)wParam;
	STickerInfo *ticker_info = (STickerInfo *)lParam;
	//m_dlgLog.AppendFormat("Tick: %d\r\n", tick->volume);
	m_nAsk = m_nAsk + 1;
	UpdateData(FALSE);
	delete tick;
	return 0;
}
